import {defineStore} from 'pinia'

export const useTodoStore = defineStore('todos',{
  state:()=>{
    return{
      // list:[
      //   {
      //     id: 1,
      //     name: '吃饭',
      //     done: false,
      //   },
      //   {
      //     id: 2,
      //     name: '睡觉',
      //     done: true,
      //   },
      //   {
      //     id: 3,
      //     name: '打豆豆',
      //     done: false,
      //   },
      // ],
      list:JSON.parse(localStorage.getItem('todoList') || '[]'),
      filter:['All','Active','Completed'],
      active:'All'
    }
  },
  actions:{
    changeDone(id){
      const todo = this.list.find(item=>item.id === id)
      todo.done =!todo.done
    },
    del(id){
      this.list = this.list.filter(item=>item.id !==id)
    },
    add(taskname){
      this.list.unshift({
        id:Date.now(),
        name:taskname,
        done:false
      })
    },
    changeAll(val){
       this.list.forEach(item=>item.done = val)
    },
    clearComplete(){
      this.list = this.list.filter(item=>!item.done)
    },
    changeActive(active){
      this.active = active
    }
  },
  getters:{
    isAll(){
      return this.list.every(item=>item.done)
    },
    leftCount(){
      return this.list.filter(item=>!item.done).length
    },
    showList(){
      if(this.active === 'Active'){
        return this.list.filter(item=>!item.done)
      }else if(this.active === 'Completed'){
        return this.list.filter(item=>item.done)
      }else{
        return this.list
      }
    }
  }
})